let extraRoutes: { path: any; component: any }[];

export function patchRoutes({ routes }) {
  // console.log(routes);
  routes.unshift({
    path: '/foo',
    // exact: true,
    component: require('@/pages/foo').default,
  });

  extraRoutes.map((item: { path: any; component: any }) => {
    routes.unshift({
      path: item.path,
      component: require(`@/pages${item.component}`).default,
    });
  });
}

export function render(oldRender: Function) {
  extraRoutes = [{ path: '/server', component: '/user2' }];

  // fetch('/api/routes').then(res => res.json()).then((res) => {
  //   extraRoutes = res.routes;
  // })
  oldRender();
}

// 在初始加载和路由切换时做一些事情，在 patchRoutes 后执行
export function onRouteChange({ location, routes, action, matchedRoutes }) {
  console.log(location);
  console.log(routes);
  console.log(action);
  console.log(matchedRoutes);

  if (matchedRoutes.length) {
    document.title =
      '借力好风-' + (matchedRoutes[matchedRoutes.length - 1].route.title || '');
  }
}
